home *** CD-ROM | disk | FTP | other *** search
/ C & C++ Multimedia Cyber Classroom / C and C++ Multimedia Cyber Classroom (Prentice Hall) (1998).iso / src / fig08_06.jar / Ch08 / Fig08_06 / Date1.cpp next >
C/C++ Source or Header  |  1997-11-09  |  3KB  |  104 lines

  1. // Fig. 8.6: date1.cpp
  2. // Member function definitions for Date class
  3. #include <iostream.h>
  4. #include "date1.h"
  5.  
  6. // Initialize static member at file scope; 
  7. // one class-wide copy.
  8. const int Date::days[] = { 0, 31, 28, 31, 30, 31, 30,
  9.                            31, 31, 30, 31, 30, 31 };
  10.  
  11. // Date constructor
  12. Date::Date( int m, int d, int y ) { setDate( m, d, y ); }
  13.  
  14. // Set the date
  15. void Date::setDate( int mm, int dd, int yy )
  16. {
  17.    month = ( mm >= 1 && mm <= 12 ) ? mm : 1;
  18.    year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;
  19.  
  20.    // test for a leap year
  21.    if ( month == 2 && leapYear( year ) )
  22.       day = ( dd >= 1 && dd <= 29 ) ? dd : 1;
  23.    else
  24.       day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;
  25. }
  26.  
  27. // Preincrement operator overloaded as a member function.
  28. Date &Date::operator++()
  29. {
  30.    helpIncrement();
  31.    return *this;  // reference return to create an lvalue
  32. }
  33.  
  34. // Postincrement operator overloaded as a member function.
  35. // Note that the dummy integer parameter does not have a
  36. // parameter name.
  37. Date Date::operator++( int )
  38. {
  39.    Date temp = *this;
  40.    helpIncrement();
  41.  
  42.    // return non-incremented, saved, temporary object
  43.    return temp;   // value return; not a reference return
  44. }
  45.  
  46. // Add a specific number of days to a date
  47. const Date &Date::operator+=( int additionalDays )
  48. {
  49.    for ( int i = 0; i < additionalDays; i++ )
  50.       helpIncrement();
  51.  
  52.    return *this;    // enables cascading
  53. }
  54.  
  55. // If the year is a leap year, return true; 
  56. // otherwise, return false
  57. bool Date::leapYear( int y )
  58. {
  59.    if ( y % 400 == 0 || ( y % 100 != 0 && y % 4 == 0 ) )
  60.       return true;   // a leap year
  61.    else
  62.       return false;  // not a leap year
  63. }
  64.  
  65. // Determine if the day is the end of the month
  66. bool Date::endOfMonth( int d )
  67. {
  68.    if ( month == 2 && leapYear( year ) )
  69.       return d == 29; // last day of Feb. in leap year
  70.    else
  71.       return d == days[ month ];
  72. }
  73.  
  74. // Function to help increment the date
  75. void Date::helpIncrement()
  76. {
  77.    if ( endOfMonth( day ) && month == 12 ) {  // end year
  78.       day = 1;
  79.       month = 1;
  80.       ++year;
  81.    }
  82.    else if ( endOfMonth( day ) ) {            // end month
  83.       day = 1;
  84.       ++month;
  85.    }
  86.    else       // not end of month or year; increment day
  87.       ++day;
  88. }
  89.  
  90. // Overloaded output operator
  91. ostream &operator<<( ostream &output, const Date &d )
  92. {
  93.    static char *monthName[ 13 ] = { "", "January",
  94.       "February", "March", "April", "May", "June",
  95.       "July", "August", "September", "October",
  96.       "November", "December" };
  97.  
  98.    output << monthName[ d.month ] << ' '
  99.           << d.day << ", " << d.year;
  100.  
  101.    return output;   // enables cascading
  102. }
  103.  
  104.